(function() {
    'use strict';

    angular
        .module('zhserverApp')
            .controller('DoctorCreateController', DoctorCreateController);

    DoctorCreateController.$inject = ['$timeout', '$scope', '$stateParams', 'entity', 'Doctor','$state'];

    function DoctorCreateController ($timeout, $scope, $stateParams, entity, Doctor,$state) {
        var vm = this;
        vm.doctor = entity;
        vm.clear = clear;
        vm.save = save;

        $timeout(function (){
            angular.element('.form-group:eq(1)>input').focus();
        });

        function clear () {
            $state.go('doctor');
        }

        function save () {
            vm.isSaving = true;
            if (vm.doctor.id !== null) {
                Doctor.update(vm.doctor, onSaveSuccess, onSaveError);
                $state.go('doctor', null, { reload: 'doctor' });

            } else {
                Doctor.save(vm.doctor, onSaveSuccess, onSaveError);
                $state.go('doctor', null, { reload: 'doctor' });

            }
        }

        function onSaveSuccess (result) {
            $scope.$emit('zhserverApp:doctorUpdate', result);
            vm.isSaving = false;
        }

        function onSaveError () {
            vm.isSaving = false;
        }


    }
})();
